package com.zzq.demo.excel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.BaseRowModel;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @description : EasyExcel 导入基类
 * @author : zhaoziqian
 * @date : 2019-06-23 17:53
 * @version : 1.0
 **/
public abstract class BaseExcelListener<T extends BaseRowModel> extends AnalysisEventListener<T> {

    private static final String SERIAL_VERSION_UID = "serialVersionUID";

    private List<T> dataList = new ArrayList<>();

    private Class<T> clazz;

    public BaseExcelListener(Class<T> clazz) {
        this.clazz = clazz;
    }

    public Class<T> getClazz() {
        return clazz;
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void clearList() {
        this.dataList.clear();
    }

    /**
     * 判断对象中属性值是否全为空
     */
    protected static boolean checkObjAllFieldsIsNull(Object object) {
        if (null == object) {
            return true;
        }
        try {
            for (Field f : object.getClass().getDeclaredFields()) {
                f.setAccessible(true);
                //只校验带ExcelProperty注解的属性
                ExcelProperty property = f.getAnnotation(ExcelProperty.class);
                if(property == null || SERIAL_VERSION_UID.equals(f.getName())){
                    continue;
                }
                if (f.get(object) != null && MyStringUtils.isNotBlank(f.get(object).toString())) {
                    return false;
                }
            }
        } catch (Exception e) {
            //do something
        }
        return true;
    }
}
